<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .el-table .warning-row {
            background: oldlace;
        }

        .el-table .success-row {
            background: #f0f9eb;
        }
    </style>
</head>
<body>
<div id="app">
<!--    多条件查询表单-->
    <el-form :inline="true" :model="searchBrand" class="demo-form-inline">
        <el-form-item label="当前状态">
            <el-select v-model="searchBrand.status" placeholder="当前状态">
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
            </el-select>
        </el-form-item>

        <el-form-item label="企业名称">
            <el-input v-model="searchBrand.companyName" placeholder="企业名称"></el-input>
        </el-form-item>

        <el-form-item label="品牌名称">
            <el-input v-model="searchBrand.brandName" placeholder="品牌名称"></el-input>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="search()">查询</el-button>
        </el-form-item>
    </el-form>
<!--    批量删除和新增按钮-->
    <el-row>
        <el-button type="danger" plain @click="BatchDelete()">批量删除</el-button>
        <el-button type="primary" @click="dialogVisible = true" plain>新增</el-button>
    </el-row>
<!--    新增对话框-->
    <el-dialog
            title="新增品牌"
            :visible.sync="dialogVisible"
            width="30%"
            >

        <el-form ref="form" :model="brand" label-width="80px">
            <el-form-item label="品牌名称">
                <el-input v-model="brand.brandName"></el-input>
            </el-form-item>
            <el-form-item label="企业名称">
                <el-input v-model="brand.companyName"></el-input>
            </el-form-item>
            <el-form-item label="排序">
                <el-input v-model="brand.ordered"></el-input>
            </el-form-item>
            <el-form-item label="备注">
                <el-input type="textarea" v-model="brand.description"></el-input>
            </el-form-item>
            <el-form-item label="状态">
                <el-switch
                        v-model="brand.status"
                        active-value="1"
                        inactive-value="0"
                >

                </el-switch>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="add">提交</el-button>
                <el-button @click="dialogVisible = false">取消</el-button>
            </el-form-item>
        </el-form>


    </el-dialog>
<!--    编辑对话框-->
    <el-dialog
            title="编辑品牌"
            :visible.sync="dialogVisible2"
            width="30%"
    >

        <el-form ref="form" :model="updateBrand" label-width="80px">
            <el-form-item label="品牌名称">
                <el-input v-model="updateBrand.brandName"></el-input>
            </el-form-item>
            <el-form-item label="企业名称">
                <el-input v-model="updateBrand.companyName"></el-input>
            </el-form-item>
            <el-form-item label="排序">
                <el-input v-model="updateBrand.ordered"></el-input>
            </el-form-item>
            <el-form-item label="备注">
                <el-input type="textarea" v-model="updateBrand.description"></el-input>
            </el-form-item>
            <el-form-item label="状态">
                <el-switch
                        v-model="updateBrand.status"
                        active-value="1"
                        inactive-value="0"
                >

                </el-switch>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="update()">提交</el-button>
                <el-button @click="dialogVisible2 = false">取消</el-button>
            </el-form-item>
        </el-form>

    </el-dialog>
    <!--    查询对话框-->
    <el-dialog
            title="查询品牌"
            :visible.sync="dialogVisible3"
            width="30%"
    >

        <el-form ref="form" :model="searchBrand" label-width="80px">
            <el-form-item label="品牌名称">
                <el-input v-model="searchBrand.brandName"></el-input>
            </el-form-item>
            <el-form-item label="企业名称">
                <el-input v-model="searchBrand.companyName"></el-input>
            </el-form-item>
            <el-form-item label="状态">
                <el-switch
                        v-model="searchBrand.status"
                        active-value="1"
                        inactive-value="0"
                >

                </el-switch>
            </el-form-item>


        </el-form>


    </el-dialog>
<!--    表格数据-->
    <template>
        <el-table
                :data="tableData"
                style="width: 100%"
                :row-class-name="tableRowClassName"
                @selection-change="handleSelectionChange"
        >
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column
                    type="index"
                    width="50">
            </el-table-column>
            <el-table-column
                    prop="brandName"
                    align ="center"
                    label="品牌名称"
                    >
            </el-table-column>
            <el-table-column
                    prop="companyName"
                    align ="center"
                    label="企业名称"
                    >
            </el-table-column>
            <el-table-column
                    prop="ordered"
                    align ="center"
                    label="排序">
            </el-table-column>
            <el-table-column
                    prop="statusStr"
                    align ="center"
                    label="当前状态">
            </el-table-column>
            <el-table-column
                    align ="center"
                    label="操作">
                <template slot-scope="scope">
                    <el-button type="primary"  @click="handleEdit(scope.row)">修改</el-button>
                    <el-button type="danger" @click="handleDelete(scope.$index,scope.row)">删除</el-button>
                </template>>
            </el-table-column>
        </el-table>
    </template>
<!--    分页工具条-->
    <div class="block">

        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="currentPage"
                :page-sizes="arr"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="totalCount">
        </el-pagination>
    </div>

</div>

<script src="js/vue.js"></script>
<script src="element-ui/lib/index.js"></script>
<link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">

<script src="js/axios-0.18.0.js"></script>
<script>
    new Vue({
        el:"#app",
        mounted(){
            this.fetchData();
        },
        methods:{
            //分页查询
            fetchData(){
                axios({
                    methods: "get",
                    url:"http://localhost/mybatis-webdemo1/brand/selectByPage?currentPage="+this.currentPage+"&pageSize="+this.pageSize
                }).then( (resp)=>{
                    //设置表格数据
                    this.tableData=resp.data.rows;
                    //设置总记录条数
                    this.totalCount=resp.data.totalCount;
                })
            },
            //删除一条记录
            handleDelete(index,row) {
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios({
                        method: "post",
                        url:"http://localhost/mybatis-webdemo1/brand/delete",
                        data:row
                    });
                }).then(() => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.selectAll();
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });

            },
            //批量删除记录
            BatchDelete(){
                this.$confirm('此操作将删除这些数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then((
                ) => {
                    //遍历选中数组的id并放在selectedIds[i]中
                    for(let i=0;i<this.multipleSelection.length;i++){
                        let selectionElements = this.multipleSelection[i];
                        this.selectedIds[i] = selectionElements.id;
                    }
                    axios({
                        method:"post",
                        url: "http://localhost:80/mybatis-webdemo1/brand/deleteByIds",
                        data:this.selectedIds
                    }).then(resp=>{
                        if (resp.data=="success"){

                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                            this.selectAll();
                        }
                        if (resp.data=="error"){
                            this.$message({
                                showClose: true,
                                message: '删除失败',
                                type: 'error'
                            });
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //添加一条记录
            add() {
                console.log(this.brand);
                //发送异步请求
                axios({
                    method: "post",
                    url:"http://localhost/mybatis-webdemo1/brand/add",
                    data:this.brand
                }).then(resp=>{
                    if (resp.data=="success"){
                        this.$message({
                            message: '添加成功',
                            type: 'success'
                        });
                        this.dialogVisible = false;
                        this.selectAll();
                        this.$forceUpdate();
                    }
                })
            },
            //修改一条记录
            update() {
                //发送异步请求
                axios({
                    method: "post",
                    url:"http://localhost/mybatis-webdemo1/brand/update",
                    data:this.updateBrand
                }).then(resp=>{
                    if (resp.data=="success"){
                        this.$message({
                            message: '修改成功',
                            type: 'success'
                        });
                        this.dialogVisible2 = false;
                        this.selectAll();
                    }
                })
            },
            selectAll() {
                axios({
                    method:"get",
                    url:"http://localhost/mybatis-webdemo1/brand/selectAll"
                }).then(resp=>{
                    this.tableData = resp.data
                })
            },
            //分页条件查询
            search(){
                console.log(this.searchBrand)
                axios({
                    method:"post",
                    url:"http://localhost/mybatis-webdemo1/brand/selectByPageAndCondition?currentPage="+this.currentPage +"&pageSize=" + this.pageSize,
                    data:this.searchBrand
                }).then(resp=>{
                    //设置表格数据
                    this.tableData =  resp.data.rows;
                    //设置总记录数
                    this.totalCount = resp.data.totalCount;
                })

            },
            handleSizeChange(val) {
                 console.log(`每页 ${val} 条`);
                //设置每一页的分页查询条数
                this.pageSize=val;
                this.fetchData();
            },
            handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
                //重新设置当前页码
                this.currentPage=val;
                this.fetchData();
            },
            handleEdit(row) {
                // 将获取的数据赋值给updateBrand(单向绑定)
                this.updateBrand = JSON.parse(JSON.stringify(row));
                //将status用toString方法转换为字符串
                this.updateBrand.status = this.updateBrand.status.toString();
                // 显示修改对话框
                this.dialogVisible2 = true;

            },

            tableRowClassName({row, rowIndex}) {
                if (rowIndex === 1) {
                    return 'warning-row';
                } else if (rowIndex === 3) {
                    return 'success-row';
                }
                return '';
            },
            //拿到选择的数据返回为数组
            handleSelectionChange(val) {
                this.multipleSelection = val;
                console.log(this.multipleSelection)
            }
        },
        //查询分页数据
        data(){
            return{
                //分页条数
                arr:[5, 10, 15, 20],
                //每页显示的条数
                pageSize:10,
                //当前页码
                currentPage:1,
                //总记录数
                totalCount:[],
                //选中的id数组
                selectedIds:[],
                //dialogVisible的值确定新增对话框是否显示
                dialogVisible: false,
                dialogVisible2: false,
                dialogVisible3: false,
                brand: {},
                updateBrand: {},
                searchBrand:{},
                //选中的记录返回为数组
                multipleSelection: [],
                tableData: [{
                //     brandName: '松鼠',
                //     companyName: '松鼠公司',
                //     ordered: 123,
                //     status:1
                // }, {
                //     brandName: '松鼠',
                //     companyName: '松鼠公司',
                //     ordered: 123,
                //     status:1
                // }, {
                //     brandName: '松鼠',
                //     companyName: '松鼠公司',
                //     ordered: 123,
                //     status:1
                // }, {
                //     brandName: '松鼠',
                //     companyName: '松鼠公司',
                //     ordered: 123,
                //     status:1
                }]
            }
        }
    })
</script>
</body>
</html>